﻿using System;
using System.Linq;
using System.Windows.Forms;
using TVRageDAL;

namespace RED
{
    public partial class EditForm : Form
    {
        public string ShowName 
        {
            get { return _showName; } 
        }
        
        public string AirDate 
        {
            get { return _airDate; }
        }
        
        public string Season 
        {
            get { return _season; }
        }
        
        public string Episode 
        {
            get { return _episode; }
        }
        
        public bool IsHd 
        {
            get { return _isHd; }
        }

        public string SearchParam
        {
            get { return _searchParam; }            
        }

        public bool FutureEpisode
        {
            get { return _futureEpisode; }
        }

        private readonly string _showName;
        private readonly string _airDate;
        private string _season;
        private string _episode;
        private bool _isHd;
        private string _searchParam;
        private bool _futureEpisode;
        private TVDAL _tvdal;
        private ShowInformation _showInfo;

        public EditForm()
        {
            InitializeComponent();
        }

        public EditForm(string showName, string airDate, string season, string episode, bool isHd, string searchParam, bool futureEpisode)
        {
            InitializeComponent();
            _showName = showName;
            _airDate = airDate;
            _season = season;
            _episode = episode;
            _isHd = isHd;
            _searchParam = searchParam;
            _futureEpisode = futureEpisode;
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            _season = seasonCounter.Value.ToString();
            _episode = episodeCounter.Value.ToString();
            _isHd = hdCheckbox.Checked;
            _searchParam = searchTextbox.Text;
            _futureEpisode = bypassCheckbox.Checked;

            DialogResult = DialogResult.OK;
            Close();
        }

        private void EditForm_Load(object sender, EventArgs e)
        {
            _tvdal = new TVDAL();
            _showInfo = _tvdal.GetShowInfo(_showName, true, true);
            showNameLabel.Text = _showName;
            airDateLabel.Text = _airDate;
            searchTextbox.Text = _searchParam;
            seasonCounter.Minimum = 1;
            episodeCounter.Minimum = 1;
            seasonCounter.Maximum = 999;
            episodeCounter.Maximum = 999;
            seasonCounter.Value = int.Parse(_season);
            episodeCounter.Value = int.Parse(_episode);
            hdCheckbox.Checked = _isHd;
            bypassCheckbox.Checked = _futureEpisode;
            var seasonCount = 0;
            var episodeCount = 0;

            if (_showInfo == null || _showInfo.Episodes == null || _showInfo.Episodes.Length <= 0) return;
            foreach (var item in _showInfo.Episodes.Where(item => item != null))
            {
                if (int.Parse(item.Season) == int.Parse(_season))
                    episodeCount++;

                seasonCount = int.Parse(item.Season);
            }

            seasonCounter.Maximum = seasonCount;
            episodeCounter.Maximum = episodeCount;
        }

        private void seasonCounter_ValueChanged(object sender, EventArgs e)
        {
            if (_showInfo == null || _showInfo.Episodes == null || _showInfo.Episodes.Length <= 0) return;
            var episodeCount = _showInfo.Episodes.Count(item => item != null && int.Parse(item.Season) == seasonCounter.Value);

            episodeCounter.Maximum = bypassCheckbox.Checked ? 999 : episodeCount;
        }

        private void bypassCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            var bs = seasonCounter.Maximum;
            var be = episodeCounter.Maximum;

            if (bypassCheckbox.Checked) {
                seasonCounter.Maximum = 999;
                episodeCounter.Maximum = 999;
            } else {
                seasonCounter.Maximum = bs;
                episodeCounter.Maximum = be;
            }
        }
    }
}
